package com.insight.spring;


import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class BeanDefintion {

    private String name;
    private Constructor<?> constructor;
    private Method postConstructMethod;
    private final List<Field> autowreidFileds;
    private Class<?> beanType;

    public BeanDefintion(Class<?> type) {
        this.beanType = type;
        Component component = type.getDeclaredAnnotation(Component.class);
        this.name = component.name().isEmpty()?  type.getSimpleName() : component.name();
        try {
            this.constructor = type.getConstructor();
            this.postConstructMethod = Arrays.stream(type.getDeclaredMethods())
                    .filter(f->f.isAnnotationPresent(PostConstruct.class))
                    .findFirst().orElse(null);
           this.autowreidFileds =
                   Arrays.stream(type.getDeclaredFields())
                           .filter(f->f.isAnnotationPresent(Autowired.class))
                           .collect(Collectors.toList());
        } catch (NoSuchMethodException e) {
            throw new RuntimeException();
        }
    }
    public String getName (){
        return name;

    }
    public Constructor<?> getConstructor(){
        return constructor;
    }

    /*用于初始换bean的生命周期*/
    public Method getPostConstructMethod (){
        return postConstructMethod;
    }
    public List<Field> getAutowridFileds(){
        return this.autowreidFileds;
    }

    public Class<?> getBeanType() {
        return beanType;
    }
}
